﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.UI;
using Kevin.Shop.Business.Products.Components.Content;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using System.Collections.Specialized;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Comments.Components;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.Modle.Product;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Comm.Validation;

namespace RiderShop.WebSite.Controllers
{
    public class ProductController : ThemeController
    {
        #region Get Product List
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="sid">分类编号</param>
        /// <param name="keyword">搜索的关键字条件</param>
        /// <param name="page">当前页的索引</param>
        /// <param name="findex">当前页的索引</param>
        /// <param name="order">按什么字段排序</param>
        /// <param name="sort">升序还是降序 -升序,0降序</param>
        /// <returns>根据条件得到的商品集合</returns>
        [HttpGet]
        public ActionResult ProductList(int sid = 0, string keyword = "", int page = 0, int findex = 2, int order = 1, int sort = 0, int hi = 0)
        {

            var model = new ProductListModel();
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                if (ValueValidate.IsNumber(keyword) && (keyword.Trim().Length == 10 || keyword.Trim().Length == 11))
                {
                    if (model.CheckProductExists(keyword))
                    {
                        return RedirectToAction("ProductDetail", "Product", new { prd = keyword });
                    }
                }
            }

            model.FiterIndex = findex;
            model.Keyword = keyword;

            var query = new ProductQuery();
            //分类和关键字条件
            query.Keyword = keyword;
            query.CategoryId = sid;
            query.PageIndex = page;
            query.IsWebsiteSearch = true;
            query.PageSize = 28;
            query.HasInventory = (hi == 1);
            query.WareHouseNumber = WebClientContext.Current.WareHouseNumber;
            //排序条件
            var sortColumn = (ProductQuery.ProductSortColumn)order;
            query.SortBy = sortColumn.ToString();
            query.SortOrder = (SortAction)sort;

            model.GetProducts(query);
            if (Request.IsAjaxRequest())
            {
                return PartialView(string.Format("~/Themes/{0}/Views/Product/ProductListPartial.cshtml", WebClientContext.Current.SiteTheme), model);
            }

            return View(WebClientContext.Current.SiteTheme, "Product", "ProductList", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
        }
        #endregion

        #region Get Product Detail
        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="prd">商品编号</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ProductDetail(string prd)
        {
            ProductModel productModel = new ProductModel();
            productModel.GetProductByProductId(prd);

            if (productModel.Product == null || string.IsNullOrEmpty(productModel.Product.ProductId))
            {
                return RedirectToAction("ProductList", "Product", new { keyword = prd });
            }

            return View(WebClientContext.Current.SiteTheme, "Product", "ProductDetail", WebClientContext.Current.CacheKeyDefaultMasterPage, productModel);
        }
        #endregion

        #region Get Product Description
        /// <summary>
        /// 得到商品评价
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <returns>partial view</returns>
        [HttpGet]
        public ActionResult ProductReviewPartial(string productId)
        {
            ProductModel productModel = new ProductModel();
            MyCommentModel myCommentModel = new MyCommentModel();
            List<ProductBasicInfoExtend> product = productModel.GetProductDescriptionByProductId(productId);
            if (product.FirstOrDefault() != null)
            {
                DbQueryResult<List<ProductReviewInfoExtend>> productBestCommentList = myCommentModel.GetProductReview(productId, 0, WebClientContext.CON_PageSize, ProductReviewType.TotalAll);
                if (!string.IsNullOrEmpty(productBestCommentList.Tag))
                {
                    var list = productBestCommentList.Tag.Split(',');
                    product[0].BestCommentCount = list.Length == 5 ? Convert.ToInt32(list[0]) : 0;//很好评论数
                    product[0].GoodCommentCount = list.Length == 5 ? Convert.ToInt32(list[1]) : 0;//好评论数
                    product[0].NormalCommentCount = list.Length == 5 ? Convert.ToInt32(list[2]) : 0;//一般评论数
                    product[0].BadCommentCount = list.Length == 5 ? Convert.ToInt32(list[3]) : 0;//差评论数
                    product[0].WorstCommentCount = list.Length == 5 ? Convert.ToInt32(list[4]) : 0;//非常差评论数
                    product[0].TotalCommentCount = product[0].BestCommentCount + product[0].GoodCommentCount + product[0].NormalCommentCount + product[0].BadCommentCount + product[0].WorstCommentCount;//总评论数

                    product[0].TotalBestCommentPage = (int)Math.Ceiling(product[0].BestCommentCount / (double)WebClientContext.CON_PageSize);//很好评论页数
                    product[0].TotalGoodCommentPage = (int)Math.Ceiling(product[0].GoodCommentCount / (double)WebClientContext.CON_PageSize);//好评论页数
                    product[0].TotalNormalCommentPage = (int)Math.Ceiling(product[0].NormalCommentCount / (double)WebClientContext.CON_PageSize);//一般评论页数
                    product[0].TotalBadCommentPage = (int)Math.Ceiling(product[0].BadCommentCount / (double)WebClientContext.CON_PageSize);//差评论页数
                    product[0].TotalWorstCommentPage = (int)Math.Ceiling(product[0].WorstCommentCount / (double)WebClientContext.CON_PageSize);//非常差评论页数


                    product[0].TotalBestAndGoodCommentPage = (int)Math.Ceiling((product[0].BestCommentCount +
                                                                  product[0].GoodCommentCount) /
                                                                 (double)WebClientContext.CON_PageSize);//很好评和好评总页数
                    product[0].TotalBadAndWorstCommentPage = (int)Math.Ceiling((product[0].BadCommentCount +
                                                                  product[0].WorstCommentCount) /
                                                                 (double)WebClientContext.CON_PageSize);//差评和非常差评总页数
                    productModel.Product = product.FirstOrDefault();
                }
            }
            return PartialView(string.Format("~/Themes/{0}/Views/Product/ProductReviewPartial.cshtml", WebClientContext.Current.SiteTheme), productModel);
        }
        #endregion

        #region Get Product Comment List
        /// <summary>
        /// 根据商品编号得到商品的评论集合
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <param name="type">评论类型(好评,中评,差评)</param>
        /// <returns>评论集合</returns>
        [HttpGet]
        public ActionResult CommentList(string productId, int pageIndex = 0, int type = 0)
        {
            MyCommentModel model = new MyCommentModel();

            model.GetProductReview(productId, pageIndex, WebClientContext.CON_PageSize, (ProductReviewType)type);

            return PartialView(string.Format("~/Themes/{0}/Views/Product/CommentListPartial.cshtml", WebClientContext.Current.SiteTheme), model);
        }
        #endregion

        #region Get Product Groupby Property List
        /// <summary>
        /// 根据商品编号得到商品的分组属性
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <returns>分组集合,每个分组里面包含了所对应的属性集合</returns>
        [HttpGet]
        public JsonResult GetProductPropertyGroup(string productId)
        {
            ProductModel productModel = new ProductModel();
            var displayPropertyList = productModel.GetProductPropertyGroup(productId);

            return Json(displayPropertyList, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        public JsonResult GetSelectPropertyProduct(string productID, string selectProperty)
        {
            ProductModel productModel = new ProductModel();
            string selectProductID = productModel.GetSelectPropertyProduct(productID, selectProperty);

            return Json(selectProductID, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Add Favority
        /// <summary>
        /// 对某个商品进行收藏
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <param name="marketPrice">市场价格</param>
        /// <returns>收藏成功/收藏失败</returns>
        [HttpGet]
        public JsonResult AddFavority(string productId, decimal unitPrice)
        {

            ProductModel productModel = new ProductModel();
            ServiceResponser serviceResponser = productModel.AddFavority(productId, unitPrice);

            return Json(serviceResponser, JsonRequestBehavior.AllowGet);
        }
        #endregion



        #region Check User Whether Already Favority
        /// <summary>
        /// 这个商品是否已经收藏
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <returns>已经收藏成功/尚未收藏</returns>
        [HttpGet]
        public JsonResult GetUserFavorityByProductId(string productId)
        {
            ServiceResponser serviceResponser = new ServiceResponser();
            MyFavoritesModel myFavoritesModel = new MyFavoritesModel();

            serviceResponser = myFavoritesModel.CheckProductAlreadyFavorite(productId);

            return Json(serviceResponser, JsonRequestBehavior.AllowGet);
        }
        #endregion


        [HttpGet]
        public ActionResult AddToCart(string prd)
        {
            return View(WebClientContext.Current.SiteTheme, "Product", "AddToCart", WebClientContext.Current.CacheKeyDefaultMasterPage, null);
        }
    }
}
